package sample.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author : kyle
 * @CreateTime : 2019/9/3
 * @Description :
 **/
@Configuration
public class RabbitConfig {

    //队列 起名：TestDirectQueue
    @Bean(RabbitConst.DirectQueue)
    public Queue testDirectQueue() {
        // durable:是否持久化,默认是true,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);

        //一般设置一下队列的持久化就好,其余两个就是默认false
        return QueueBuilder.nonDurable(RabbitConst.DirectQueue).autoDelete().build();
    }

    @Bean(RabbitConst.FanoutQueue)
    public Queue testFanoutQueue() {
        return QueueBuilder.nonDurable(RabbitConst.FanoutQueue).autoDelete().build();
    }

    @Bean(RabbitConst.FanoutQueue2)
    public Queue testFanoutQueue2() {
        return QueueBuilder.nonDurable(RabbitConst.FanoutQueue2).autoDelete().build();
    }

    @Bean(RabbitConst.TopicQueueA)
    public Queue testTopicQueueA() {
        return QueueBuilder.nonDurable(RabbitConst.TopicQueueA).autoDelete().build();
    }

    @Bean(RabbitConst.TopicQueueB)
    public Queue testTopicQueueB() {
        return QueueBuilder.nonDurable(RabbitConst.TopicQueueB).autoDelete().build();
    }

    //Direct交换机 起名：TestDirectExchange
    @Bean(RabbitConst.DirectExchange)
    public DirectExchange testDirectExchange() {
        //  return new DirectExchange("TestDirectExchange",true,true);
        return ExchangeBuilder.directExchange(RabbitConst.DirectExchange).autoDelete().build();
    }

    @Bean(RabbitConst.FanoutExchange)
    public FanoutExchange testFanoutExchange() {
        return ExchangeBuilder.fanoutExchange(RabbitConst.FanoutExchange).autoDelete().build();
    }

    @Bean(RabbitConst.TopicExchange)
    public TopicExchange testTopicExchange() {
        return ExchangeBuilder.topicExchange(RabbitConst.TopicExchange).autoDelete().build();
    }

    //绑定  将队列和交换机绑定, 并设置用于匹配键：TestDirectRouting
    @Bean
    Binding bindingDirect(@Qualifier(RabbitConst.DirectQueue) Queue testDirectQueue,
                          @Qualifier(RabbitConst.DirectExchange) DirectExchange testDirectExchange) {
        return BindingBuilder.bind(testDirectQueue).to(testDirectExchange).with(RabbitConst.DirectRoutingKey);
    }

    @Bean
    Binding bindingFanout1(@Qualifier(RabbitConst.FanoutQueue) Queue testFanoutQueue,
                           @Qualifier(RabbitConst.FanoutExchange) FanoutExchange testFanoutExchange) {
        return BindingBuilder.bind(testFanoutQueue).to(testFanoutExchange);
    }

    @Bean
    Binding bindingFanout2(@Qualifier(RabbitConst.FanoutQueue2) Queue testFanoutQueue,
                           @Qualifier(RabbitConst.FanoutExchange) FanoutExchange testFanoutExchange) {
        return BindingBuilder.bind(testFanoutQueue).to(testFanoutExchange);
    }

    @Bean
    Binding bindingTopicA(@Qualifier(RabbitConst.TopicQueueA) Queue queue,
                          @Qualifier(RabbitConst.TopicExchange) TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("NEWS.*");
    }

    @Bean
    Binding bindingTopicB(@Qualifier(RabbitConst.TopicQueueB) Queue queue,
                          @Qualifier(RabbitConst.TopicExchange) TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("*.ADD");
    }

}